package com.rfid.a252robot.activity.presenter;

import android.os.Handler;

import com.rfid.a252robot.activity.view.IBookInfoView;
import com.rfid.a252robot.activity.view.IBookListView;
import com.rfid.a252robot.activity.view.IDoorBoxView;
import com.rfid.a252robot.base.BasePresenter;
import com.rfid.a252robot.bean.Box;
import com.rfid.a252robot.bean.Row;
import com.rfid.a252robot.event.BookQueryEvent;
import com.rfid.a252robot.event.BoxOpenEvent;
import com.rfid.a252robot.event.OffShelfEvent;
import com.rfid.a252robot.event.OnShelfEvent;
import com.rfid.a252robot.task.OffShelfTask;
import com.rfid.a252robot.task.OnShelfTask;
import com.rfid.db.bean.Book;
import com.rfid.manager.DBManager;
import com.rfid.manager.DoorBoxManager;
import com.rfid.manager.TaskManager;
import com.rfid.util.Util;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class DoorBoxPresenter  extends BasePresenter<IDoorBoxView> implements DoorBoxManager.Listener {

    Handler mHandler = new Handler();

    @Override
    public void onAttach(IDoorBoxView view) {
        super.onAttach(view);
        System.out.println("onAttach");
        EventBus.getDefault().register(this);
        DoorBoxManager.INSTANCE.addListener(this);
    }

    @Override
    public void detach() {
        super.detach();
        System.out.println("detach");
        EventBus.getDefault().unregister(this);
        DoorBoxManager.INSTANCE.removeListener(this);
    }

    //最长时间36*2秒 + 读取标签时间
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOffShelfEnd(OffShelfEvent event) {
        System.out.println("onOffShelfEnd");
        if (mView != null) {
            mView.onOffShelfEnd();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShelfStatus(OnShelfEvent event) {
        if (mView != null) {
            if (event.getStatus() == OnShelfEvent.STATUS_CHECKING) {
                mView.onShelfChecking(event.getBoxNum());
            } else if (event.getStatus() == OnShelfEvent.STATUS_END) {
                mView.onShelfEnd(event.getBoxNum());
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBookUpdate(BookQueryEvent event) {
        if (mView != null) {
            mView.onBook(event.getBook());
        }
    }

    public List<Row> getRows() {
        List<Row> data = new ArrayList<>();
        for (int i=0;i<4;i++) {
            List<Integer>  boxNums = Util.getBoxes(i + 1);
            Row row = new Row();
            List<Box> boxes = new ArrayList<>();
            row.setBoxes(boxes);
            row.setNum(i + 1);
            for (Integer boxNum : boxNums) {
                Box box = new Box();
                box.setRowNum(i + 1);
                box.setNum(boxNum);
                box.setBooks(DBManager.getBooks(box.getNum()));
                boxes.add(box);
            }
            data.add(row);
        }
        return data;
    }

    @Override
    public void onLockOpen(int num, boolean success) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mView != null) {
                    mView.onBoxOpen(num,success);
                }
            }
        });
    }

    @Override
    public void onLockOpenStatus(int num, boolean open) {

    }

    public void checkOffshelf() {
        TaskManager.instance().submit(new OffShelfTask(0,true));
    }

    public void checkOnShelf(int boxNum) {
        OnShelfTask task = new OnShelfTask(boxNum);
        TaskManager.instance().submit(task);
    }
}
